'use strict';
var clientWidgets = angular.module('clientWidgets');
var clientApp = angular.module('clientApp');

//clientWidgets.controller('listThumbCtrl', function ($scope, $rootScope) {
//    $scope.myTree = [];
//    $scope.myAlert = function () {
//        alert('1');
//    };
//});

clientApp.directive('listThumbWidget', ['$rootScope', 'Constant', 'Util', 'iScrollUtil', 'DomUtil', 'WidgetUtil',
    function($rootScope, Constant, Util, iScrollUtil, DomUtil, WidgetUtil) {

    return {
        templateUrl: '/ext/themes/flat/widget/list/list-thumb/view/main.html',
        replace: true,
        //transclude: true,
        restrict: 'E',
        //scope: true,
        scope: {
            vo: '=vo',
            propMap: '=propModel',
            contrlAct: '&',
            itemSelect: '&'
        },
        link: function(scope, element, attrs) {

            scope.select = function(item) {
                scope.$emit(scope.vo.code + '_select', item);
            };

            var display = function () {
                if (scope.action == 'reset') {
                    scope.items = [];
                    scope.allData = [];
                }
                var newVo = WidgetUtil.convert(scope.vo, scope.propMap);

                scope.items = scope.items.concat(newVo.data);
                scope.allData = scope.allData.concat(newVo.data);

                if(scope.myScroll) {
                    setTimeout(function () {
                        scope.myScroll.refresh();
                    }, 10);
                }
            };

            console.log('link function entered');
            scope.action = 'reset';
            scope.allData = [];
            scope.items = [];

            $(element).find('.wrapper').height(Util.getScreenSize().h + 'px');
            if(!scope.myScroll) {
                setTimeout(function() {
                    iScrollUtil.initIscroll(scope, element);
                }, 20);
            };

            scope.$watch('vo', function (newVal, oldVal) {
                if (newVal) {
                    display();
                    if (newVal.status == Constant.HTTP_RETURN_CODE_NO_MORE) {
                        scope.noMore = true;
                    } else {
                        scope.noMore = false;
                    }
                    iScrollUtil.noMoreOrNot(scope, element);
                }
            });
        }
    };
}]);
